package com.senyint.imsdk.http;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.senyint.imsdk.util.StringUtil;

public class SimpleHttpRequest implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3917146213369733350L;
	public static final int REQUEST_GET = 0 ;
	public static final int REQUEST_POST = 1 ;
	public static final int REQUEST_UPLOAD = 2 ;
	public static final int REQUEST_DOWN = 3 ;
	
	private int requestCode = 0 ;
	private int requestType = REQUEST_POST ;
	private String url  ;
	private Map<String, String> parameter = new HashMap<String,String>();
	private Map<String, File> files  = new HashMap<String,File>();
	private ISimpleHttpService.HttpCallback callback ;
	private String destFile ;
	
	public int getRequestCode() {
		return requestCode;
	}
	public SimpleHttpRequest setRequestCode(int requestCode) {
		this.requestCode = requestCode;
		return this ;
	}
	public int getRequestType() {
		return requestType;
	}
	public SimpleHttpRequest setRequestType(int requestType) {
		this.requestType = requestType;
		return this;
	}
	public String getUrl() {
		return url;
	}
	public SimpleHttpRequest setUrl(String url) {
		this.url = url;
		return this;
	}
	public Map<String, String> getParameter() {
		return parameter;
	}
	public void setParameter(Map<String, String> parameter) {
		this.parameter = parameter;
	}
	public Map<String, File> getFiles() {
		return files;
	}
	public void setFiles(Map<String, File> files) {
		this.files = files;
	}
	
	public String getDestFile() {
		return destFile;
	}
	public SimpleHttpRequest setDestFile(String destFile) {
		this.destFile = destFile;
		return this ;
	}
	public ISimpleHttpService.HttpCallback getCallback() {
		return callback;
	}
	public SimpleHttpRequest setCallback(ISimpleHttpService.HttpCallback callback) {
		this.callback = callback;
		return this ;
	}
	
	public SimpleHttpRequest addParameter(String key,String value){
		parameter.put(key, value);
		return this ;
	}
	
	public SimpleHttpRequest addFile(String key,File value){
		if(value != null)
			files.put(key, value);
		return this ;
	}
	
	public String getFlag(){
		if(url == null) return null ;
		
        return StringUtil.getMD5(url+_getParameterString());
    }
	
	private String _getParameterString(){
        StringBuilder bulider = new StringBuilder();
        if (parameter != null && parameter.size() > 0) {
			for (Map.Entry<String, String> entry : parameter.entrySet()) {
				bulider.append(entry.getKey()).append("=");
				try {
					bulider.append(URLEncoder.encode(entry.getValue(), HttpHeader.ENCODING_UTF8));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					return null ;
				}
				bulider.append("&");
			}
			bulider.deleteCharAt(bulider.length() - 1);
        }
        return bulider.toString();
    }
	
}
